package category

import (
	"github.com/xinliangnote/go-gin-api/internal/pkg/core"
	"github.com/xinliangnote/go-gin-api/internal/repository/mongodb/category"
	"github.com/xinliangnote/go-gin-api/internal/repository/mongodb/product"
)

type CreateCategoryData struct {
	Id   string
	Name string
	Desc string
}

func (s *service) Create(ctx core.Context, productData *CreateCategoryData) (id string, err error) {
	model := category.NewModel()
	model.Name = productData.Name
	model.Desc = productData.Desc

	id, err = model.Create(s.db.GetDb())
	if err != nil {
		return "", err
	}
	return id, err
}

func (s *service) List(ctx core.Context, pageIndex uint, pageSize uint) (productList []category.Category, err error) {
	model := category.NewModel()
	list, err := model.List(s.db.GetDb(), pageIndex, pageSize)
	return list, err
}

func (s *service) Update(ctx core.Context, productData *CreateCategoryData) error {
	model := category.NewModel()
	model.Name = productData.Name
	model.Desc = productData.Desc
	model.Id = productData.Id
	err := model.Update(s.db.GetDb())
	return err

}

func (s *service) Delete(ctx core.Context, productData *CreateCategoryData) error {
	model := category.NewModel()
	model.Id = productData.Id
	model.IsDel = 1
	err := model.Update(s.db.GetDb())
	return err
}

func (s *service) Count(ctx core.Context) (int64, error) {
	model := product.NewModel()
	count, err := model.Count(s.db.GetDb())
	return count, err
}
